﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace OneMake.Model
{
    //各品类销售
    public class report_type_sale
    {
        public string type { get; set; }
        public Dictionary<DateTime, int> sale_nums { get; set; }
        public Dictionary<DateTime, double> sale_amounts { get; set; }

        public report_type_sale()
        {
            sale_nums = new Dictionary<DateTime, int>();
            sale_amounts = new Dictionary<DateTime, double>();
        }
    }

    partial class ReportHelper
    {
        public string get_short_shop_name(int shopId)
        {
            var db = new PetaPoco.Database("mysql" + UserSession.Network);
            string sql = "select name from conf_shop where shopid=@0";
            string shopName = db.SafeExecuteScalar<string>(sql, shopId);
            if (!string.IsNullOrEmpty(shopName))
                shopName = shopName.Substring(0, 3);
            return shopName;
        }

        public report_type_sale get_report_type_sale(int shopId, string type, DateTime beginTime, DateTime endTime)
        {
            var db = new PetaPoco.Database("mysql" + UserSession.Network);
            db.OneTimeCommandTimeout = int.MaxValue;
            //string shortName = get_short_shop_name(shopId);
            string sql = string.Format(@"select b.catelog,a.stat_datetime,sum(a.sub_n_buycount) as sale_num, round(sum(a.sub_actual_amount),2) as sale_amount from hdp_d_deal a
                        left join product_archive b on a.sub_ext_shop_sku = b.sku
                        where (a.sub_status='WAIT_BUYER_CONFIRM_GOODS' or a.sub_status='WAIT_SELLER_SEND_GOODS' or a.sub_status='TRADE_FINISHED')
                        and (b.catelog='{0}' and b.shopid={1}) 
                        and (a.stat_datetime between '{2}' and '{3}')", type, shopId, beginTime.ToShortDateString(), endTime.ToShortDateString());

            report_type_sale report = new report_type_sale();
            report.type = type;

            IDataReader reader = db.ExecuteReader(sql);
            while (reader.Read())
            {
                object date = reader["stat_datetime"];
                object saleNum = reader["sale_num"];
                object saleAmount = reader["sale_amount"];
                if (!Convert.IsDBNull(date))
                {
                    report.sale_nums.Add(System.Convert.ToDateTime(date), System.Convert.ToInt32(saleNum));
                    report.sale_amounts.Add(System.Convert.ToDateTime(date), System.Convert.ToDouble(saleAmount));
                }
            }
            return report;
        }
        
        //裤
        public List<report_type_sale> get_report_gplxs_sale_num_ku(int shopId, DateTime beginTime, DateTime endTime)
        {
            System.Diagnostics.Debug.Assert(beginTime <= endTime);

            string[] types = { "牛仔裤", "西裤", "休闲裤" };

            List<report_type_sale> reports = new List<report_type_sale>();          
            foreach (string type in types)
            {
                report_type_sale report = get_report_type_sale(shopId, type, beginTime, endTime);
                reports.Add(report);
            }
            
            //计算总计
            report_type_sale reportAll = new report_type_sale();
            reportAll.type = "--裤--";
            
            foreach (report_type_sale report in reports)
            {
                foreach (KeyValuePair<DateTime, int> val in report.sale_nums)
                {
                    if (!reportAll.sale_nums.ContainsKey(val.Key))
                        reportAll.sale_nums.Add(val.Key, 0);
                    reportAll.sale_nums[val.Key] += val.Value;
                }

                foreach (KeyValuePair<DateTime, double> val in report.sale_amounts)
                {
                    if (!reportAll.sale_amounts.ContainsKey(val.Key))
                        reportAll.sale_amounts.Add(val.Key, 0);
                    reportAll.sale_amounts[val.Key] += val.Value;
                }

            }
            reports.Insert(0, reportAll);

            return reports;
        }


        //饰品
        public List<report_type_sale> get_report_gplxs_sale_num_shipin(int shopId, DateTime beginTime, DateTime endTime)
        {
            System.Diagnostics.Debug.Assert(beginTime <= endTime);

            string[] types = { "钱包", "袜" };

            List<report_type_sale> reports = new List<report_type_sale>();
            foreach (string type in types)
            {
                report_type_sale report = get_report_type_sale(shopId, type, beginTime, endTime);
                reports.Add(report);
            }

            //计算总计
            report_type_sale reportAll = new report_type_sale();
            reportAll.type = "--配件--";

            foreach (report_type_sale report in reports)
            {
                foreach (KeyValuePair<DateTime, int> val in report.sale_nums)
                {
                    if (!reportAll.sale_nums.ContainsKey(val.Key))
                        reportAll.sale_nums.Add(val.Key, 0);
                    reportAll.sale_nums[val.Key] += val.Value;
                }

                foreach (KeyValuePair<DateTime, double> val in report.sale_amounts)
                {
                    if (!reportAll.sale_amounts.ContainsKey(val.Key))
                        reportAll.sale_amounts.Add(val.Key, 0);
                    reportAll.sale_amounts[val.Key] += val.Value;
                }

            }
            reports.Insert(0, reportAll);

            return reports;
        }
        
        //衣
        public List<report_type_sale> get_report_gplxs_sale_num_yi(int shopId, DateTime beginTime, DateTime endTime)
        {
            System.Diagnostics.Debug.Assert(beginTime <= endTime);

            string[] types = { "长袖T恤", "长袖衬衫", "短袖T恤", "短袖衬衫", "风衣", "棉服", "夹克", "西装", "羊毛衣", "羽绒服" };

            List<report_type_sale> reports = new List<report_type_sale>();
            foreach (string type in types)
            {
                report_type_sale report = get_report_type_sale(shopId, type, beginTime, endTime);
                reports.Add(report);
            }

            //计算总计
            report_type_sale reportAll = new report_type_sale();
            reportAll.type = "--衣--";

            foreach (report_type_sale report in reports)
            {
                foreach (KeyValuePair<DateTime, int> val in report.sale_nums)
                {
                    if (!reportAll.sale_nums.ContainsKey(val.Key))
                        reportAll.sale_nums.Add(val.Key, 0);
                    reportAll.sale_nums[val.Key] += val.Value;
                }

                foreach (KeyValuePair<DateTime, double> val in report.sale_amounts)
                {
                    if (!reportAll.sale_amounts.ContainsKey(val.Key))
                        reportAll.sale_amounts.Add(val.Key, 0);
                    reportAll.sale_amounts[val.Key] += val.Value;
                }

            }
            reports.Insert(0, reportAll);

            return reports;
        }

        public List<report_type_sale> get_report_gplxs_sale_num(int shopId, DateTime beginTime, DateTime endTime)
        {
            System.Diagnostics.Debug.Assert(beginTime <= endTime);

            List<report_type_sale> reports = new List<report_type_sale>();
            reports.AddRange(get_report_gplxs_sale_num_ku(shopId,beginTime,endTime));
            reports.AddRange(get_report_gplxs_sale_num_shipin(shopId,beginTime,endTime));
            reports.AddRange(get_report_gplxs_sale_num_yi(shopId, beginTime, endTime));

            //计算总计
            report_type_sale reportAll = new report_type_sale();
            reportAll.type = "--总计--";

            foreach (report_type_sale report in reports)
            {
                if (report.type != "--裤--" && report.type != "--配件--" && report.type != "--衣--")
                    continue;

                foreach (KeyValuePair<DateTime, int> val in report.sale_nums)
                {
                    if (!reportAll.sale_nums.ContainsKey(val.Key))
                        reportAll.sale_nums.Add(val.Key, 0);
                    reportAll.sale_nums[val.Key] += val.Value;
                }

                foreach (KeyValuePair<DateTime, double> val in report.sale_amounts)
                {
                    if (!reportAll.sale_amounts.ContainsKey(val.Key))
                        reportAll.sale_amounts.Add(val.Key, 0.0);
                    reportAll.sale_amounts[val.Key] += val.Value;
                }
            }
            reports.Add(reportAll);

            return reports;
        }
    }
}
